//
//  AddressListViewController.m
//  penzer
//
//  Created by caiyc on 2018/10/16.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "AddressListViewController.h"
#import "AddressListCell.h"
//#import "YWAddressViewController.h"
#import "AddadressViewController.h"
@interface AddressListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray *dataSouce;
@property(nonatomic,strong)NSString *selctId;
@end

@implementation AddressListViewController
-(NSMutableArray *)dataSouce{
    if(!_dataSouce){
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}
-(void)viewWillAppear:(BOOL)animated{
    [self loadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigation:1 andTitle:@"收货地址" andNaviColor:[UIColor clearColor] andTitleColor:nil];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
   // Do any additional setup after loading the view from its nib.
}
-(void)loadData{
    [self.dataSouce removeAllObjects];
    NSDictionary *paramter = @{@"op":@"address",@"ctrl":@"live",@"ac":@"member",@"ta":@"index"};
    [self weqPostHandle:paramter success:^(id response) {
        if(response){
            [self.dataSouce addObjectsFromArray:response[@"data"]];
            [self.tableView reloadData];
            NSArray *datas = response[@"data"];
            if(datas.count==0){
                [WToast showWithText:@"暂无地址"];
            }
        }
    } failure:^(NSError *error) {
        
    }];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.needBack){
    self.selecCity(self.dataSouce[indexPath.row]);
    [self.navigationController popViewControllerAnimated:1];
    self.selctId = [NSString stringWithFormat:@"%@", self.dataSouce[indexPath.row][@"id"]];
    [self defaultAddress];
    }else{
        
    }
}
-(void)defaultAddress{
    NSDictionary *paramter = @{@"op":@"address",@"ctrl":@"live",@"ac":@"member",@"ta":@"address_default",@"id":self.selctId};
    [self weqPostHandle:paramter success:^(id response) {
        if(response){
           
        }
    } failure:^(NSError *error) {
        
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSouce.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"cell";
    AddressListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if(!cell){
        cell = [[[NSBundle mainBundle]loadNibNamed:@"AddressListCell" owner:self options:nil]lastObject];
    }
    [cell bindData:self.dataSouce[indexPath.row]];
    __weak typeof(self) weakSelf = self;
    cell.dele=^(){
        NSString *ids = self.dataSouce[indexPath.row][@"id"];
        [weakSelf deleAdd:ids];
    };
    cell.edit=^(){
        NSDictionary *address = self.dataSouce[indexPath.row];
        AddadressViewController *vc = [[AddadressViewController alloc]init];
        vc.AddressDic = address;
        [weakSelf pushSecondVC:vc];
    };
    return cell;
}
-(void)deleAdd:(NSString *)address_Id{
    NSDictionary *paramter = @{@"op":@"address",@"ctrl":@"live",@"ac":@"member",@"ta":@"del_address",@"id":address_Id};
    [self weqPostHandle:paramter success:^(id response) {
        if(response){
            [self loadData];
        }
    } failure:^(NSError *error) {
        
    }];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)addAddress:(Regular_Btn *)sender {
    AddadressViewController *vc = [[AddadressViewController alloc]init];
    [self pushSecondVC:vc];
}
@end
